<?php
/**
 * <https://y.st./>
 * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

$xhtml = array(
	'<{title}>' => 'Tape.java',
	'<{subtitle}>' => 'Written in <span title="Programming 2">CS 1103</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-07-05',
	'<{copyright year}>' => '2017',
	'takedown' => '2017-11-01',
	'<{body}>' => <<<END
<h2>Source code:</h2>
<blockquote>
<pre><code>/* 
 * Copyright (C) 2017 Alex Yst
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
 */
package turing;

/**
 * Alex Yst&apos;s CS 1103 (Programming 2) Unit 3 submission
 * 
 * This class represents a tape of unlimited cells. A single cell exists in
 * memory to begin with, but more are added as needed.
 * 
 * @author Alex Yst
 */
public class Tape {
/**
 * This is the current working cell. All operations are performed in the
 * context of this cell.
 */
	private Cell currentCell;
/**
 * This is the cell at the &quot;beginning&quot; of the tape.
 */
	private Cell beginning;
/**
 * This method should usually never be needed. It returns the current working
 * cell, and with it, the whole known section of the tape. Use this method with
 * care.
 * 
 * @return The current working cell
 */
	public Cell getCurrentCell() {
		return currentCell;
	}
/**
 * This method returns the character content of the current working cell.
 * 
 * @return The content of the current working cell
 */
	public char getContent() {
		return currentCell.content;
	}
/**
 * This method sets the character of the current working cell.
 * 
 * @param ch The character to set
 */
	public void setContent(char ch) {
		currentCell.content = ch;
	}
/**
 * Sets the new current working cell to be the cell previous of the current
 * current working cell, adding a cell if need be.
 */
	public void moveLeft() {
		if(currentCell.prev == null) {
			currentCell.prev = new Cell();
			currentCell.prev.next = currentCell;
			currentCell.prev.content = &apos; &apos;;
			beginning = currentCell.prev;
		}
		currentCell = currentCell.prev;
	}
/**
 * Sets the new current working cell to be the cell after of the current
 * current working cell, adding a cell if need be.
 */
	public void moveRight() {
		if(currentCell.next == null) {
			currentCell.next = new Cell();
			currentCell.next.prev = currentCell;
			currentCell.next.content = &apos; &apos;;
		}
		currentCell = currentCell.next;
	}
/**
 * This method returns the contents of the known section of the tape, in string
 * form.
 * 
 * @return The known contents of the tape
 */
	public String getTapeContents() {
		String s = &quot;&quot;;
		Cell current = beginning;
		while(current != null) {
			s += current.content;
			current = current.next;
		}
		return s;
	}
/**
 * Constructor method. Just sets the default state of the tape to something
 * sensible.
 */
	public Tape() {
		currentCell = new Cell();
		currentCell.content = &apos; &apos;;
		beginning = currentCell;
	}
}</code></pre>
</blockquote>
<h2>Test output:</h2>
<h3><code>TestTape.java</code>:</h3>
<blockquote>
<pre><code>Tape Conents:  Hello World 
Final position at the W</code></pre>
</blockquote>
<h3><code>TestTapeGUI.java</code>:</h3>
<img src="/y.st./source/y.st./static/img/CC_BY-SA_4.0/y.st./coursework/CS1103/Type.java.png" alt="Test Tape" class="framed-centred-image"/>
<h3><code>TestTuringMachine.java</code>:</h3>
<blockquote>
<pre><code>Running machine #1.  Output should be:  Hello
Actual output is:                       Hello 

Running machine #2.  Should throw an IllegalStateExcpetion.
Caught Illegal Argument Exception, with error message:
Cannot find an applicable rule; tape contents =  ERROR

Running machine #3.  Output should be: aababbbababbabbaba aababbbababbabbaba
Actual output is:                      aababbbababbabbaba aababbbababbabbaba</code></pre>
</blockquote>
END
);
